package com.meilai.project.controller.web.market;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.controller.web.market.dto.*;
import com.meilai.project.controller.web.market.service.BillMarketService;
import com.meilai.project.controller.web.market.vo.BillMarketForADMakeVO;
import com.meilai.project.controller.web.market.vo.BillMarketVO;
import com.meilai.project.controller.web.market.vo.ConfirmBillDetail;
import com.meilai.project.controller.web.market.vo.MarketBillVO;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.interceptor.NoAuth;
import com.meilai.project.util.ThreadLocalUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenxing
 */
@RestController
@RequestMapping("api/web/bill-market")
@Api(tags = "市场单")
public class BillMarketController {

    @Autowired
    private BillMarketService service;

    @GetMapping("list-latest-for-ad-make-by-contract-id")
    @ApiOperation(value = "根据contractId查市场列表(for制作单)")
    public ApiResult<List<BillMarketForADMakeVO>> listForADMakeByContractId(@ApiParam("contractId") Long contractId) {
        List<BillMarketForADMakeVO> regularApplyVOPage = service.listForADMakeByContractId(contractId);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list-latest-for-ad-make-by-customer-id")
    @ApiOperation(value = "根据customerId查市场列表(for制作单)")
    public ApiResult<List<BillMarketForADMakeVO>> listForADMakeByCustomerId(@ApiParam("customerId") Long customerId) {
        List<BillMarketForADMakeVO> regularApplyVOPage = service.listForADMakeByCustomerId(customerId);
        return ApiResult.ok(regularApplyVOPage);
    }

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<BillMarketVO>> list(@ApiParam("form") BillMarketListDTO req) {
        req.setCurrentUserId(ThreadLocalUserUtil.get().getId());
        Page<BillMarketVO> billMarketVOPage = service.selectPageList(req);

        return ApiResult.ok(billMarketVOPage);
    }

    @PutMapping("generate-confirm-bill")
    @ApiOperation(value = "生成确认单(返回批次号batchStr)")
    public ApiResult<String> generateConfirmBill(@RequestBody GenerateConfirmBillDto req) {
        String batchStr = service.generateConfirmBill(req);
        return ApiResult.ok(batchStr);
    }

    //客户确认单详情
    @NoAuth
    @GetMapping("confirm-bill-detail")
    @ApiOperation(value = "客户确认单详情（无需登录）")
    public ApiResult<ConfirmBillDetail> confirmBillDetail(@RequestParam(value = "batchStr") String batchStr) {
        return ApiResult.ok(service.confirmBillDetail(batchStr));
    }

    @NoAuth
    @PutMapping("sign-confirm-bill")
    @ApiOperation(value = "确认单签字（无需登录）")
    public ApiResult<Boolean> signConfirmBill(@RequestBody SignConfirmBillDTO req) {
        service.signConfirmBill(req);
        return ApiResult.ok();
    }

    @GetMapping("detail")
    @ApiOperation(value = "单据明细")
    public ApiResult<BillMarketVO> detail(
            @ApiParam("id") @RequestParam(value = "id", required = false) Long id
    ) {
        return ApiResult.ok(service.getOneById(id));
    }

    @PostMapping("upload-design-pic")
    @ApiOperation("上传设计稿")
    public ApiResult<Boolean> uploadDesignPic(@ApiParam("json") @RequestBody BillMarketDesignPicDto req) {
        try {
            service.uploadDesignPic(req);
            return ApiResult.ok();
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PostMapping("modify-shoot-requirement")
    @ApiOperation("修改拍照要求")
    public ApiResult<Boolean> modifyShootRequirement(
            @ApiParam("json") @RequestBody ShootRequirementDTO req
    ) {
        try {
            service.modifyShootRequirement(req);
            return ApiResult.ok();
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PostMapping("confirm-shoot-requirement")
    @ApiOperation("确认拍照要求")
    public ApiResult<Boolean> confirmShootRequirement(
            @ApiParam("id") @RequestParam(value = "id", required = true) Long id
    ) {
        try {
            service.confirmShootRequirement(id);
            return ApiResult.ok();
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@ApiParam("json") @RequestBody BillMarketDTO req) {
        try {
            if (service.save(req)) {
                return ApiResult.ok("新增成功");
            } else {
                return ApiResult.err("新增失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("begin")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@ApiParam("json") @RequestBody BillMarketDTO req) {
        try {
            if (service.begin(req)) {
                return ApiResult.ok("提交成功");
            } else {
                return ApiResult.err("提交失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(@ApiParam("json") @RequestBody BillMarketCheckDTO req) {
        try {
            if (service.pass(req)) {
                return ApiResult.ok("通过成功");
            } else {
                return ApiResult.err("通过失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("return_back")
    @ApiOperation("驳回")
    public ApiResult<Boolean> returnBack(@ApiParam("json") @RequestBody BillMarketCheckDTO req) {
        try {
            if (service.returnBack(req)) {
                return ApiResult.ok("驳回成功");
            } else {
                return ApiResult.err("驳回失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "逻辑删除")
    public ApiResult<Boolean> delete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.delete(id)) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("actual_delete")
    @ApiOperation(value = "物理删除")
    public ApiResult<Boolean> actualDelete(@RequestParam("id") @ApiParam("id") Long id) {
        try {
            if (service.actualDelete(id)) {
                return ApiResult.ok("彻底删除成功");
            } else {
                return ApiResult.err("彻底删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    /*==================================NEW====================================*/

    @PostMapping("save-new")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@ApiParam("json") @RequestBody MarketBillDTO req) {
        try {
            if (service.save(req)) {
                return ApiResult.ok("新增成功");
            } else {
                return ApiResult.err("新增失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("begin-new")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@ApiParam("json") @RequestBody MarketBillDTO req) {
        try {
            if (service.begin(req)) {
                return ApiResult.ok("提交成功");
            } else {
                return ApiResult.err("提交失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @GetMapping("detail-new")
    @ApiOperation(value = "单据明细")
    public ApiResult<MarketBillVO> detailNew(@ApiParam("id") @RequestParam(value = "id", required = false) Long id) {
        return ApiResult.ok(service.getSingleOneById(id));
    }

    @GetMapping("sub-detail")
    @ApiOperation(value = "单据明细已生成")
    public ApiResult<MarketBillVO> subDetail(@ApiParam("marketDetailId") @RequestParam(value = "marketDetailId", required = false) Long marketDetailId) {
        return ApiResult.ok(service.getSubOneByDetailId(marketDetailId));
    }

    @GetMapping("init-market-bill")
    @ApiOperation(value = "新建市场单初始化单据明细")
    public ApiResult<MarketBillVO> initMarketBill(@ApiParam("saleBillId") @RequestParam(value = "saleBillId", required = false) Long saleBillId,
                                                  @ApiParam("saleBillDetailId") @RequestParam(value = "saleBillDetailId", required = false) Long saleBillDetailId) {

        return ApiResult.ok(service.initSingleOneById(saleBillId,saleBillDetailId));
    }

    @GetMapping("list-new")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<MarketBillVO>> listNew(@ApiParam("form") BillMarketListDTO req) {
        req.setCurrentUserId(ThreadLocalUserUtil.get().getId());
        Page<MarketBillVO> billMarketVOPage = service.queryPageList(req);
        return ApiResult.ok(billMarketVOPage);
    }

}
